/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipercube.impl.simple;

import com.ipercube.Dimension;
import com.ipercube.model.Record;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author tartur
 */
public class SimpleRecord implements Record {

    private Serializable id;
    private Map<Dimension, Object> values;
    private Map<Serializable, Dimension> dimById;

    public SimpleRecord(Serializable id, Dimension[] dimensions) {
        this.id = id;
        values = new HashMap<Dimension, Object>();
        dimById = new HashMap<Serializable, Dimension>();
        for (Dimension dimension : dimensions) {
            values.put(dimension, null);
            dimById.put(dimension.getId(), dimension);
        }
    }

    public void setValue(Object v, Dimension d) {
        if (!values.containsKey(d)) {
            throw new RuntimeException("This record does not have this dimension : "+d);
        }
        values.put(d, v);
    }

    public Object getValue(Dimension d) {
        if (!values.containsKey(d)) {
            throw new RuntimeException("This record does not have this dimension : "+d);
        }
        return values.get(d);
    }

    public Object getValue(Serializable dimensionId) {
        Dimension d = dimById.get(dimensionId);
        return getValue(d);
    }

    public Serializable getId() {
        return id;
    }
}
